package org.xxpay.pay.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.xxpay.core.common.constant.PayConstant;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.entity.PayOrderLog;
import org.xxpay.pay.service.CommonService;

import javax.jms.Queue;
import java.util.Date;

/**
 * @Description: 订单日志信息
 * @author pangxiaoyu
 * @date 2020-02-24
 */
@Component
public class Mq4PayOrderLog {

	private static final MyLog _log = MyLog.getLog(Mq4PayOrderLog.class);

	@Autowired
	private CommonService commonService;

	@Autowired
	private Queue payOrderLogQueue;

	@Autowired
	private JmsTemplate jmsTemplate;
	/**
	 * 发送MQ消息
	 * @param msg
	 */
	public void send(String msg) {
		_log.debug("发送MQ消息:msg={}", msg);
		this.jmsTemplate.convertAndSend(this.payOrderLogQueue, msg);
	}

	/**
	 * 接收MQ消息
	 * @param msg
	 */
	@JmsListener(destination = MqConfig.PAY_ORDER_LOG_QUEUE_NAME)
	@Async("mqExecutor")
	public void receive(String msg) {
		String logPrefix = "【支付订单日志处理】";
		_log.debug("{}接收消息:msg={}", logPrefix, msg);
		JSONObject msgJson = JSON.parseObject(msg);
        Byte type = msgJson.getByte("type");
        if (type == PayConstant.PAY_ORDER_LOG_TYPE_MCH_PAYORDER) {
            // 移除类型字段
            msgJson.remove("type");
            // 获取商户订单号和支付订单号
            String params = msgJson.getString("params");
            String payOrderId = msgJson.getString("payOrderId");
            JSONObject paramsJson = JSON.parseObject(params);
            String mchOrderNo = paramsJson.getString("mchOrderNo");
            msgJson.remove("payOrderId");
            // 保存下单数据
            this.createPayOrderLog(payOrderId, mchOrderNo, PayConstant.PAY_ORDER_LOG_TYPE_MCH_PAYORDER, msgJson.toJSONString());
        } else if (type == PayConstant.PAY_ORDER_LOG_TYPE_ORDER_RESPONSE) {
            // 获取商户订单号和支付订单号
            String params = msgJson.getString("params");
            String payOrderId = msgJson.getString("payOrderId");
            String mchOrderNo = msgJson.getString("mchOrderNo");
            // 保存下单响应数据
            this.createPayOrderLog(payOrderId, mchOrderNo, PayConstant.PAY_ORDER_LOG_TYPE_ORDER_RESPONSE, params);
        }
	}

    /**
     *
     * @param payOrderId
     * @param mchOrderNo
     * @param type
     * @param content
     */
    public void createPayOrderLog(String payOrderId, String mchOrderNo, Byte type, String content) {

        // 判断类型、数据内容是否为空
        if (type == null || StringUtils.isEmpty(content)) {
            _log.error("订单日志类型或参数内容为空：type={}, content={}", type, content);
        }
        try {
            // 执行数据保存
            PayOrderLog payOrderLog = new PayOrderLog();
            payOrderLog.setPayOrderId(payOrderId);
            payOrderLog.setMchOrderNo(mchOrderNo);
            payOrderLog.setContent(content);
            payOrderLog.setType(type);
            payOrderLog.setBusinessCreateTime(new Date());
            commonService.payOrderLogService.add(payOrderLog);
            _log.debug("type={},订单日志保存完成", type);
        }catch (Exception e) {
            _log.error(e, "订单日志保存失败");
        }
    }


}
